package nancal.mp.mvc

import nancal.mp.db.mongo.extend.getAppDeploySetting
import nancal.mp.db.mongo.mor
import nbcp.base.annotation.mvc.*
import nbcp.base.extend.*
import org.springframework.web.bind.annotation.*
import java.net.InetAddress
import javax.servlet.http.HttpServletRequest

@RestController
@OpenAction
class OpenController {
    @GetMapping("/open/dns")
    fun dns(name: String): String {
        var ip = InetAddress.getByName(name);
        return ip.hostAddress;
    }

    @GetMapping("/open/app-setting")
    fun app(code: String): String {
        return mor.dev.appDeploySetting.queryByCode(code).toEntity().ToJson()
    }

    @GetMapping("/open/cache-setting")
    fun appc(code: String): String {
        return mor.dev.appDeploySetting.getAppDeploySetting(code).ToJson()
    }


    @RequestMapping(value = arrayOf(
            "/header/{p1}",
            "/header/{p1}/{p2}",
            "/header/{p1}/{p2}/{p3}",
            "/header/{p1}/{p2}/{p3}/{p4}",
            "/header/{p1}/{p2}/{p3}/{p4}/{p5}",
            "/header/{p1}/{p2}/{p3}/{p4}/{p5}/{p6}",
            "/header/{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}",
            "/header/{p1}/{p2}/{p3}/{p4}/{p5}/{p6}/{p7}/{p8}",
    ), method = arrayOf(RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE))
    fun header(
            @PathVariable(required = true) p1: String,
            @PathVariable(required = false) p2: String?,
            @PathVariable(required = false) p3: String?,
            @PathVariable(required = false) p4: String?,
            @PathVariable(required = false) p5: String?,
            @PathVariable(required = false) p6: String?,
            @PathVariable(required = false) p7: String?,
            @PathVariable(required = false) p8: String?,
            request: HttpServletRequest
    ): String {
        return request.headerNames.toList().map {
            it + ": " + request.getHeader(it)
        }.joinToString("\n")
    }

}